<!DOCTYPE html>
<html>

<head>
    <title>socket.io Test</title>
    <!--socket.io-->
    <script type="text/javascript" src="http://cdn.staticfile.org/socket.io/4.6.1/socket.io.js"></script>
</head>

<body>
    <h1>socket.io Test</h1>
    <p>Welcome to socket.io Test</p>
    <button onclick="a()">按钮</button>
    <ul id="ultext">

    </ul>
</body>

<script type="text/javascript">
    var socket = io.connect('http://localhost:3001');

    // 发送click1事件
    function a() {
        socket.emit('click1', {test: 'data'});
    }

    //监听click2事件  
    socket.on('click2', function (data) {
        console.log(data)
        var htmldatas = data.datas;
        var html = '';
        for (var i = 0; i < htmldatas.length; i++) {
            var htmldata = htmldatas[i];
            html += '<li>' + htmldata + '</li>';
        }
        document.getElementById('ultext').innerHTML = html;
    })

    // 监听全局广播
    socket.on('all', function (e) {
        console.log('监听全局广播', e)
    })
</script>

</html>